package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.patchmanage.back.annotation.OperLog;
import com.patchmanage.back.entity.Role;
import com.patchmanage.back.exception.SystemException;
import com.patchmanage.back.result.CodeMsg;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-04-15
 */
@Api(value = " 角色 controller", tags = {"角色 Api"})
@RestController
@RequestMapping("/back/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;

    /**
     * 获取角色列表
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取角色列表接口", notes = "获取角色列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pId", value = "pId", required = false, dataType = "Integer", paramType = "body")
    })
    public Result<JSONArray> list(@RequestBody Dict dict) {
        Integer pId = dict.getInt("pId");
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
        if(pId != null){
            queryWrapper.eq("p_id",pId);
        }
        List<Role> groupList = roleService.list(queryWrapper);
        return Result.success(treeRoleList(JSONUtil.parseArray(groupList),0));
    }

    private JSONArray treeRoleList(JSONArray RoleList, int parentId) {
        JSONArray childRole = new JSONArray();
        for (Object object : RoleList) {
            JSONObject jsonRole = JSONUtil.parseObj(object);
            int RoleId = jsonRole.getInt("id");
            int pid = jsonRole.getInt("pId");
            if (parentId == pid) {
                JSONArray c_node = treeRoleList(RoleList, RoleId);
                jsonRole.putOnce("childNode", c_node);
                childRole.add(jsonRole);
            }
        }
        return childRole;
    }

    @ApiOperation(value = "新增角色列表接口", notes = "新增角色列表接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @OperLog(operModul="角色",operDesc="新增角色")
    public Result<Boolean> saveOrUpdate(@RequestBody Role role){
        return Result.success(roleService.saveOrUpdate(role));
    }

    @ApiOperation(value = "删除角色接口", notes = "删除角色接口")
    @RequestMapping(value = "removeByIds", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids", required = true, dataType = "String", paramType = "body")
    })
    @OperLog(operModul="角色",operDesc="删除角色")
    public Result<Boolean> removeByIds(@RequestBody Dict dict){
        String id = dict.getStr("ids");
        String[] ids = id.split(",");

        for(String roleId : ids){
            QueryWrapper q  = new QueryWrapper();
            q.eq("p_id",roleId);
            if(roleService.count(q) > 0){
                throw new SystemException(CodeMsg.PATCH_EXIST_ERROR);
            }
        }

        return Result.success(roleService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation(value = "获取角色接口", notes = "获取角色接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "body")
    })
    public Result<Role> getById(@RequestBody Dict dict){
        String id = dict.getStr("id");
        return Result.success(roleService.getById(id));
    }

}
